{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "#coding=utf-8"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### OSM-overpass服务接口使用，在线查询[OpenStreetMap](http:www.openstreetmap.org)开放空间数据库。\n",
    "**_ by [openthings@163.com](http://my.oschina.net/u/2306127/blog), 2016-04-23. _**  \n",
    ">#### overpy-使用overpass api接口的python library，这里将返回结果集保存为JSON格式。  \n",
    "* 安装：$ pip install overpy\n",
    "* 文档：http://python-overpy.readthedocs.org/en/latest/example.html#basic-example  \n",
    "* 接口：http://wiki.openstreetmap.org/wiki/Overpass_API  \n",
    "本工具例程基于上述文档例程进行编写。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os, sys, gc\n",
    "import time\n",
    "import json\n",
    "\n",
    "import overpy\n",
    "from pprint import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 调用overpass接口，获取result数据结果集。\n",
    "* 由于通过网络返回，容易中断，而且是在内存中处理，不适合创建大的查询集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#范围：纬度1，经度1，纬度2，经度2\n",
    "#返回：result\n",
    "def get_osm():\n",
    "    query = \"[out:json];node(50.745,7.17,50.75,7.18);out;\"\n",
    "    osm_op_api = overpy.Overpass()\n",
    "    result = osm_op_api.query(query)\n",
    "\n",
    "    print(\"Nodes: \",len(result.nodes))\n",
    "    print(\"Ways: \",len(result.ways))\n",
    "    print(\"Relations: \",len(result.relations))\n",
    "    return result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 在线获取osm数据."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes:  2267\n",
      "Ways:  0\n",
      "Relations:  0\n"
     ]
    }
   ],
   "source": [
    "result = get_osm()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 显示node的属性信息（仅显示前3个node的信息）。"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "nodeset = result.nodes[0:3]\n",
    "pprint(nodeset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 遍历node的子集，该子集由上一步产生。"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "for n in nodeset:\n",
    "    print(n.id,n.lat,n.lon)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 将查询到的数据集合转换为json格式并写入json格式的文件（_ 该格式可由Spark直接载入: SQLContext.read.json()_ ）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def node2json(node):\n",
    "    jsonNode=\"{\\\"id\\\":\\\"%s\\\", \\\"lat\\\":\\\"%s\\\", \\\"lon\\\":\\\"%s\\\"}\"%(node.id,node.lat,node.lon)\n",
    "    return jsonNode\n",
    "\n",
    "def node2jsonfile(fname,nodeset):\n",
    "    fnode = open(fname,\"w+\")\n",
    "    for n in nodeset:\n",
    "        jn = node2json(n) + \"\\n\"\n",
    "        fnode.write(jn)\n",
    "    fnode.close()\n",
    "    print(\"Nodes:\",len(nodeset),\", Write to: \",fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 执行json文件保存操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes: 2267 , Write to:  ../data/overpass.osm_node.json\n"
     ]
    }
   ],
   "source": [
    "node2jsonfile(\"../data/overpass.osm_node.json\",result.nodes)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 保存为GeoJSON格式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
